package com.ibeetl.admin.api.web;

import com.ibeetl.admin.api.config.RedisUtil;
import com.ibeetl.admin.api.entity.Travel_user;
import com.ibeetl.admin.api.service.SMSService;
import com.ibeetl.admin.api.util.TokenUtils;
import com.ibeetl.admin.core.web.JsonResult;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
@RestController
@Tag(
        name = "SMSController")
public class SMSController {
    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/api/sms";
    @Resource
    SMSService smsService;
    @Resource
    RedisUtil redisUtil;



    @Operation(
        summary = "获取短信验证码"
    )
    @PostMapping(MODEL + "/getSMSCode.json")
    @CrossOrigin
    public JsonResult<Travel_user> getCode(String phone) {
        String sms_code = smsService.getSMSCode(phone);
        Travel_user apiUser = new Travel_user();
        apiUser.setPhone(phone);
        apiUser.setSmsCode(sms_code);
        return JsonResult.success(apiUser);
    }

    @PostMapping(MODEL + "/checkSMSCode.json")
    @CrossOrigin
    @Operation(
            summary = "验证短信"
    )
    public JsonResult<Travel_user> checkSMSCode(Travel_user apiUser) {
        long timeout;
        if(apiUser.isRememberMeExpiration()){
            timeout = 604800;// 7天
        }else {
            timeout = 86400;  // 1天
        }
        String token = TokenUtils.createToken();
        apiUser.setToken(token);
        redisUtil.set(apiUser.getPhone(),token,timeout);
        return JsonResult.success(apiUser);
    }





}
